///////////////////////////////////////////////////////////////
//
// FileName : kunpackager.cpp
// Creator : BingXi Li
// Date : 2009-07-14 09:48:07
// Comment :
//
///////////////////////////////////////////////////////////////

#include "global.h"
#include "kunpackager.h"
#include "kunpackercontrol.h"

using namespace KCS0014;

KUnpacker::KUnpacker()
{
	m_pkUI = NULL;
}

KUnpacker::~KUnpacker()
{

}

bool KUnpacker::Init(IUnpackagerUI *pkUI)
{
	bool bResult = false;
	PROCESS_ERROR(pkUI != NULL);

	m_pkUI = pkUI;

	bResult = true;

_KCS0014_PROCESS_EXIT_LABEL:
	return bResult;
}

int KUnpacker::Unpack(const wchar_t *szSrcFile, const wchar_t *szDestFolder)
{
	int bResult = _KCS0014_UNKNOWN_ERROR;
	const int nParamCount = 3;
	char* pachParams[nParamCount];
	int nSrcFileLen = 0;
	int nDestFolderLen = 0;

	KUnPackerControl kController;

	PROCESS_ERROR(szSrcFile != NULL);
	PROCESS_ERROR(szDestFolder != NULL);
	PROCESS_ERROR(m_pkUI != NULL);

	pachParams[0] = NULL;
	pachParams[1] = NULL;
	pachParams[2] = NULL;

	nSrcFileLen = wcslen(szSrcFile);
	nDestFolderLen = wcslen(szDestFolder);

	pachParams[1] = new char[nSrcFileLen * 2 + 1];
	memset(pachParams[1], 0, (nSrcFileLen * 2 + 1) * sizeof(char));
	WideCharToMultiByte(CP_ACP, 0, szSrcFile, nSrcFileLen, pachParams[1], nSrcFileLen * 2, NULL, NULL);

	pachParams[2] = new char[nDestFolderLen * 2 + 1];
	memset(pachParams[2], 0, (nDestFolderLen * 2 + 1) * sizeof(char));
	WideCharToMultiByte(CP_ACP, 0, szDestFolder, nDestFolderLen, pachParams[2], nDestFolderLen * 2, NULL, NULL);

	//PROCESS_ERROR(kController.Init(m_pkUI));
	bResult = kController.ParseParam(pachParams, nParamCount);

_KCS0014_PROCESS_EXIT_LABEL:
	if (pachParams[1] != NULL)
		delete[] pachParams[1];
	if (pachParams[2] != NULL)
		delete[] pachParams[2];
	return bResult;
}